package com.changpay.zywy.ui.activity.mine

import android.Manifest
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.help.ImageHelper
import com.changpay.zywy.help.PermissionHelper
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.utils.DialogUtil
import com.changpay.zywy.utils.ImageLoader
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import kotlinx.android.synthetic.main.activity_ren_zhen.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.HashMap


class RenZhenActivity : BaseActivity(), IView {


    private var selectImageType = 0
    private var uploadType = 0
    private var compressPath: String = "" // 身份证首页图片路径
    private var compressPathTwo: String = "" // 身份证背面图片路径
    private lateinit var UploadPath: String  // 上传身份证首页图片路径
    private lateinit var UploadPathTwo: String // 上传身份证背面图片路径

    override fun attachLayoutRes(): Int {
        return R.layout.activity_ren_zhen
    }

    private val mDialog by lazy {
        DialogUtil.getWaitDialog(this, "实名认证中")
    }

    override fun showLoading() {
        mDialog.show()
    }

    override fun hideLoading() {
        mDialog.dismiss()
    }


    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        // 用于处理上传图片的回调
        if (uploadType == 0) {
            UploadPath = msg
            uploadType++
            ImageHelper.upLoadImage(this, token, compressPathTwo)
        } else if (uploadType == 1) {
            UploadPathTwo = msg
            uploadType = 0
            indent()
        }
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun initData() {}

    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.renzhen_submit -> submit()
            R.id.iv_qr_home -> {
                selectImageType = 0
                PermissionHelper.instance.getPermission(this, object : PermissionHelper.PermissionCallBack {
                    override fun onSuccess() {
                        ImageHelper.showImageChoose(this@RenZhenActivity)
                    }

                    override fun onFail() {
                        showToast("请先允许权限再使用此功能")
                    }
                }, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            }
            R.id.iv_qr_back -> {
                selectImageType = 1
                PermissionHelper.instance.getPermission(this, object : PermissionHelper.PermissionCallBack {
                    override fun onSuccess() {
                        ImageHelper.showImageChoose(this@RenZhenActivity)
                    }

                    override fun onFail() {
                        showToast("请先允许权限再使用此功能")
                    }
                }, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            }

        }
    }

    override fun initView() {
        tv_title.text = "实名认证"
        iv_back.setOnClickListener(onClickListener)
        renzhen_submit.setOnClickListener(onClickListener)
        iv_qr_home.setOnClickListener(onClickListener)
        iv_qr_back.setOnClickListener(onClickListener)
    }

    override fun start() {

    }

    private fun submit() {
        val name = renzhen_name.getInputText()
        if (TextUtils.isEmpty(name)) {
            showToast("请输入名称")
            return
        }
        val card = renzhen_qr_code.getInputText()
        if (TextUtils.isEmpty(card)) {
            showToast("请输入银行卡号")
            return
        }
        if (TextUtils.isEmpty(compressPath)) {
            showToast("请拍摄身份证人像页")
            return
        }
        if (TextUtils.isEmpty(compressPathTwo)) {
            showToast("请拍摄身份证国徽页")
            return
        }
        upLoadImage()
    }

    private fun upLoadImage() {
        ImageHelper.upLoadImage(this, token, compressPath)
    }

    private fun indent() {
        val params = HashMap<String, String>()
        params["key"] = token
        params["truename"] = renzhen_name.getInputText()
        params["idcard"] = renzhen_qr_code.getInputText()
        params["pic_before"] = UploadPath
        params["pic_after"] = UploadPathTwo
        RetrofitHelper.service.member_approve(params).sss(this)
        {
            showToast(it.result.toString())
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    val localMedias = PictureSelector.obtainMultipleResult(data)
                    if (selectImageType == 0) {
                        compressPath = localMedias[0].path
                        ImageLoader.loadImage(iv_qr_home, compressPath)
                    } else {
                        compressPathTwo = localMedias[0].path
                        ImageLoader.loadImage(iv_qr_back, compressPathTwo)
                    }
                }
            }
        }
    }
}
